package com.ehotting.edsta.cae.service.impl;

import com.alibaba.fastjson.JSON;
import com.ehotting.edsta.cae.config.CaeProperties;
import com.ehotting.edsta.cae.domain.CommonRequest;
import com.ehotting.edsta.cae.domain.CommonResponse;
import com.ehotting.edsta.cae.domain.ComputeSendParamsDto;
import com.ehotting.edsta.cae.domain.RequestDto;
import com.ehotting.edsta.cae.service.ICaeService;
import com.ehotting.eaf.core.constant.CacheConstants;
import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.eaf.security.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class CaeServiceImpl implements ICaeService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CaeProperties caeProperties;

    @Override
    public CommonResponse rePostService(CommonRequest request) {
        OkHttpUtils okHttpUtils = packOkHttpUtils(request);
        log.info("into rePostService>>>>>");
        String sync = null;
        Object object = request.getRequestBody();

        if ("post".equalsIgnoreCase(request.getRequestMethod())) {
            sync = okHttpUtils.addBody(object).postBody(true).sync();
        }
        if ("get".equalsIgnoreCase(request.getRequestMethod())){
            String string = JSON.toJSONString(object);
            Map<String, String> map = JSON.parseObject(string, Map.class);
            sync = okHttpUtils.addParam(map).get().sync();
        }
        Map<String, Object> webInfo = request.getWebInfo();
        return packCommonResponse(sync, webInfo);
    }

    private CommonResponse packCommonResponse(String sync, Map<String, Object> webInfo) {
        Map<String,Object> customResponse = null;
        if (sync != null) {
            customResponse = JSON.parseObject(sync, Map.class);
        }
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setWebInfo(webInfo);
        commonResponse.setResponse(customResponse);
        return commonResponse;
    }

    private OkHttpUtils packOkHttpUtils(CommonRequest request) {
        String requestUrl = caeProperties.getUrl() + request.getRequestUrl();
        log.info("requestUrl:{}",requestUrl);
        return OkHttpUtils.builder().url(requestUrl)
                    .addHeader(request.getHeaders())
                    .addHeader("authorization", tokenService.getJwtByPriKey("","him2wsx#EDC"));
    }

    @Override
    public CommonResponse rePostUploadService(CommonRequest request) {
        log.info("into rePostUploadService>>>>>");
        OkHttpUtils okHttpUtils = packOkHttpUtils(request);

        String contentType = request.getHeaders().get("contentType");
        String url = request.getRequestUrl();
        String sync = null;
        if ("multipart/form-data".equalsIgnoreCase(contentType)) {
            // 文件上传
            String string = JSON.toJSONString(request.getRequestBody());
            if (url.contains("files/uploadFolder")){
                // 如果是上传文件夹
                sync = okHttpUtils.uploadDir(string);
            }else {
                // 单个文件上传
                Map<String, String> map = JSON.parseObject(string, Map.class);
                String fileContent = new String(Base64.getDecoder().decode(map.get("fileContent")));
                map.put("fileContent", fileContent);
                sync = okHttpUtils.addParam(map).uploadFile();
            }
        }else {
            sync = okHttpUtils.addBody(request.getRequestBody()).postBody(true).sync();
        }
        Map<String, Object> webInfo = request.getWebInfo();
        return packCommonResponse(sync, webInfo);
    }
}
